﻿using DevExpress.XtraEditors.Controls;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_Teacher_Option : BaseForm
    {
        string TeacherID = string.Empty;
        ServiceBase service = new ServiceBase();
        TB_Exam_Examiner model = null;
        public Base_Teacher_Option()
        {
            InitializeComponent();
        }

        public Base_Teacher_Option(string strTeacherID)
        {
            InitializeComponent();
            TeacherID = strTeacherID;
            cmbKGType.Properties.NullText = "";
            cmbSSZY.Properties.NullText = "";

            BindSpecialityKinds();
            BindSpecialityKindsDetail();
            

            if (TeacherID.ToSafeString().Length > 0)
            {
                model = service.GetExaminerModel(TeacherID);
                if (model != null)
                {
                    model.Attach();
                    txtLoginName.Text = model.LoginName; 
                    txtPassword.Text = model.Password.ToSafeDecrypt();
                    txtName.Text = model.ExaminerName;
                    cmbSSZY.EditValue = model.SpecialityKindsNO;
                    cmbKGType.EditValue = model.SpecialityKinds_DetailNO;

                    if (model.IsActive == "1")
                    {
                        chkSFYX.Checked = true;
                    }
                    else
                    {
                        chkSFYX.Checked = false;
                    }
                    txtBZ.Text = model.Remark;
                }
            }
        }
        private void BindSpecialityKinds()
        {
            this.cmbSSZY.Properties.NullText = "";
            this.cmbSSZY.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKindsNO", "专业代码", 10));
            this.cmbSSZY.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKindsName", "专业名称"));
            this.cmbSSZY.Properties.DataSource = service.GetSpecialityKindsTable("","","","");
            this.cmbSSZY.Properties.DisplayMember = "SpecialityKindsName";
            this.cmbSSZY.Properties.ValueMember = "SpecialityKindsNO";
        }

        private void BindSpecialityKindsDetail()
        {
            this.cmbKGType.Properties.Columns.Clear();
            this.cmbKGType.Properties.DataSource = null;
            this.cmbKGType.Properties.NullText = "";
            if (cmbSSZY.EditValue == null || cmbSSZY.EditValue.ToSafeString().Length == 0)
            {
                return;
            }
            this.cmbKGType.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKinds_DetailNO", "考官代码"));
            this.cmbKGType.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKinds_DetailName", "考官类型"));
            this.cmbKGType.Properties.DataSource = service.GetSpecialityKindsDetailTable(cmbSSZY.EditValue.ToSafeString());
            this.cmbKGType.Properties.DisplayMember = "SpecialityKinds_DetailName";
            this.cmbKGType.Properties.ValueMember = "SpecialityKinds_DetailNO";
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (txtLoginName.Text.ToSafeString().Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先输入登陆名!");
                this.txtLoginName.Focus();
                return;
            }
            if (txtPassword.Text.ToSafeString().Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先输入密码!");
                this.txtPassword.Focus();
                return;
            }
            if (txtName.Text.ToSafeString().Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先输入名称!");
                this.txtName.Focus();
                return;
            }
            if (cmbSSZY.Text == "" || cmbSSZY.EditValue == null || cmbSSZY.EditValue.ToSafeString().Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择所属专业!");
                this.cmbSSZY.Focus();
                return;
            }
            if (cmbKGType.Text == "" || cmbKGType.EditValue == null || cmbKGType.EditValue.ToSafeString().Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择考官类型!");
                this.cmbKGType.Focus();
                return;
            }
            if (model == null)
            {
                model = new TB_Exam_Examiner();
                model.CreateTime = DateTime.Now;
            }
            model.Remark = txtBZ.Text;
            model.ExamYear = SessionerHelper.SystemYear;
            model.IsActive = chkSFYX.Checked ? "1" : "0";
            model.LoginName = txtLoginName.Text;
            model.ExaminerName = txtName.Text;
            model.Password = txtPassword.Text.ToSafeEncrypt();
            model.SpecialityKindsNO = cmbSSZY.EditValue.ToSafeString();
            model.SpecialityKinds_DetailNO = cmbKGType.EditValue.ToSafeString();
            if (cmbSSZY.Text == "" || cmbSSZY.EditValue == null || cmbSSZY.EditValue.ToSafeString().Length == 0)
            {
                model.SpecialityKindsNO = string.Empty;
            }
            else
            {
                model.SpecialityKindsNO = cmbSSZY.EditValue.ToSafeString();
            }
            if (cmbKGType.Text == "" || cmbKGType.EditValue == null || cmbKGType.EditValue.ToSafeString().Length == 0)
            {
                model.SpecialityKinds_DetailNO = string.Empty;
            }
            else
            {
                model.SpecialityKinds_DetailNO = cmbKGType.EditValue.ToSafeString();
            }
            if (service.SaveExaminer(model))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbSSZY_EditValueChanged(object sender, EventArgs e)
        {
            BindSpecialityKindsDetail();
        }
    }
}
